package cn.silver.module.oss.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.core.validate.AddGroup;
import cn.silver.framework.core.validate.EditGroup;
import cn.silver.framework.core.validate.QueryGroup;
import cn.silver.framework.idempotent.annotation.RepeatSubmit;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.log.enums.OperateTypeEnums;
import cn.silver.framework.mybatis.page.PageQuery;
import cn.silver.framework.openapi.model.Result;
import cn.silver.framework.openapi.model.ResultPage;
import cn.silver.framework.web.controller.ApiController;
import cn.silver.module.oss.model.SysOssConfigVo;
import cn.silver.module.oss.param.SysOssConfigBo;
import cn.silver.module.oss.service.ISysOssConfigService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 对象存储配置
 *
 * @author Lion Li
 * @author 孤舟烟雨
 * @date 2021-08-13
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/resource/oss/config")
public class SysOssConfigController extends ApiController {

    private final ISysOssConfigService ossConfigService;

    /**
     * 查询对象存储配置列表
     */
    @SaCheckPermission("system:oss:list")
    @GetMapping("/list")
    public ResultPage<SysOssConfigVo> list(@Validated(QueryGroup.class) SysOssConfigBo bo, PageQuery pageQuery) {
        return ossConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取对象存储配置详细信息
     *
     * @param ossConfigId OSS配置ID
     */
    @SaCheckPermission("system:oss:query")
    @GetMapping("/{ossConfigId}")
    public Result<SysOssConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ossConfigId) {
        return Result.ok(ossConfigService.queryById(ossConfigId));
    }

    /**
     * 新增对象存储配置
     */
    @SaCheckPermission("system:oss:add")
    @Log(title = "对象存储配置", operate = OperateEnums.ALTER_SAVE)
    @RepeatSubmit()
    @PostMapping()
    public Result<Void> add(@Validated(AddGroup.class) @RequestBody SysOssConfigBo bo) {
        ossConfigService.insertByBo(bo);
        return Result.ok();
    }

    /**
     * 修改对象存储配置
     */
    @SaCheckPermission("system:oss:edit")
    @Log(title = "对象存储配置", operate = OperateEnums.ALTER_UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public Result<Void> edit(@Validated(EditGroup.class) @RequestBody SysOssConfigBo bo) {
        ossConfigService.updateByBo(bo);
        return Result.ok();
    }

    /**
     * 删除对象存储配置
     *
     * @param ossConfigIds OSS配置ID串
     */
    @SaCheckPermission("system:oss:remove")
    @Log(title = "对象存储配置", operate = OperateEnums.ALTER_DELETE)
    @DeleteMapping("/{ossConfigIds}")
    public Result<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ossConfigIds) {
        ossConfigService.deleteWithValidByIds(List.of(ossConfigIds), true);
        return Result.ok();
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("system:oss:edit")
    @Log(title = "对象存储状态修改", operate = OperateEnums.ALTER_UPDATE)
    @PutMapping("/changeStatus")
    public Result<Void> changeStatus(@RequestBody SysOssConfigBo bo) {
        ossConfigService.updateOssConfigStatus(bo);
        return Result.ok();
    }
}
